<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/10/28
 * Time: 17:04
 */

namespace app\MAPP\shop\ctl;

use app\MAPP\shop\mode\lists\func;
use core\lib\BaseControlerDB;
use core\lib\Config;
use function core\p;
use function core\pplain;
use Extentions\template\Template;
use Extentions\upload\Upload;
use app\MAPP\shop\mode\goodmanager\func as lism;

class goodmmc extends BaseControlerDB
{
    var $mode;
    public function __USERFUNC()
    {
        $this->mode = new lism();
        $this->conf=Config::all("SQLlib\\shoping\\misc");
        if (!($this->msg->ip == "::1" || $this->msg->ip=="127.0.0.1")){
            backto2("/run/index/index");
        }
    }

    function index($page = 0){
        if (is_null($page)){$page = 0;}
        $tpl = Template::Newinstance();
        $tpl->displayN("bar.php",__DIR__);
        $tmp = $this->mode->listedit($page);
        $tpl->assign("data",date("Y M D h:m:s"));
        $tpl->assign("shop",$tmp);
        $tpl->assign("pagen",$page+10);
        if (($page-10)<0){
            $tpl->assign("pageb",0);
        }else{
            $tpl->assign("pageb",$page-10);
        }
        $tpl->assign("page",(int)$page/10);
        $tpl->displayFd("edithome.php",__DIR__);
    }

    function addgoods(){
        $tpl = Template::Newinstance();
        $tpl->assign("data","123");
        $tpl->displayN("bar.php",__DIR__);
        $tpl->displayFd("goodadd.php",__DIR__);

        if ($this->msg->isFile()){
            $up = Upload::Newinstance($this->conf["uppath"]);
            $up->fsrc = $this->msg->Fileparms();
            $filename = time().".".$up->ext;
            $num = $this->msg->Postaparm("num");
            $price = $this->msg->Postaparm("price");
            $this->mode->addgoods($this->msg->Postaparm("goodname")
                ,$this->msg->Postaparm("content") ,
                is_numeric($num)?$num:0,
                is_numeric($price)?$num:0,
                $this->conf["imgpath"].$filename);
            $up->SetFilename($filename);
            $up->uploadf();
        }
    }

    function editgoog($id){
        $tpl = Template::Newinstance();
        $tpl->assign("data","123");
        $tpl->displayN("bar.php",__DIR__);
        $temp = $this->mode->getinfo($id)[0];
        $tpl->assign("goodname",$temp["name"]);
        $tpl->assign("num",$temp["num"]);
        $tpl->assign("price",$temp["price"]);
        $tpl->assign("cont",$temp["info"]);
        $tpl->displayFd("goodedit.php",__DIR__);

        if (!$this->msg->isPost()){
            return;
        }
        p($this->msg->Postparms());
        $this->mode->updategoods(
            $id,
            $this->msg->Postaparm("goodname"),
            $this->msg->Postaparm("num"),
            $this->msg->Postaparm("price"),
            $this->msg->Postaparm("content")
        );

    }

}